﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kristall;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Kristall.Managers;

namespace Kristall.Screens
{
    public enum Buttons
    {
        Ok,
        YesNo,
        YesNoCancel,
    }

    public class MessageScreen : GameScreen
    {
        string message;
        public static BasicEffect AlphaEffect;

        public event EventHandler OnYes;
        public event EventHandler OnNo;
        public event EventHandler OnCancel;
        public event EventHandler OnOk;

        List<MenuEntry> MenuEntries = new List<MenuEntry>();
        int selectedEntry = 0;
        Color backgroundColor = Color.White * 0.5f;

        public MessageScreen(string message, Buttons buttons)
        {
            IsPopup = true;
            this.message = message;


            this.TransitionOnTime = this.TransitionOffTime = TimeSpan.FromSeconds(0.2f);

            MenuEntry yes = new MenuEntry("Yes");
            MenuEntry no = new MenuEntry("No");
            MenuEntry cancel = new MenuEntry("Cancel");
            MenuEntry ok = new MenuEntry("Ok");

            yes.OnSelect += new EventHandler(yes_OnSelect);
            no.OnSelect += new EventHandler(no_OnSelect);
            cancel.OnSelect += new EventHandler(cancel_OnSelect);
            ok.OnSelect += new EventHandler(ok_OnSelect);

            switch (buttons)
            {
                case Buttons.Ok:
                    MenuEntries.Add(ok);
                    break;
                case Buttons.YesNo:
                    MenuEntries.Add(yes);
                    MenuEntries.Add(no);
                    break;
                case Buttons.YesNoCancel:
                    MenuEntries.Add(yes);
                    MenuEntries.Add(no);
                    MenuEntries.Add(cancel);
                    break;
                default:
                    break;
            }

        }

        void ok_OnSelect(object sender, EventArgs e)
        {
            if (OnOk != null)
            {
                OnOk(this, null);
            }
            this.ExitScreen();
        }

        public override void LoadContent()
        {
            this.selectedEntry = 0;

            if (AlphaEffect == null)
                AlphaEffect = new BasicEffect(ScreenManager.GraphicsDevice);

            //Setup effect to act as the default SpriteBatch effect
            Matrix projection = Matrix.CreateOrthographicOffCenter(0, Kristall.Viewport.X, Kristall.Viewport.Y, 0, 0, 1);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            AlphaEffect.World = Matrix.Identity;
            AlphaEffect.View = Matrix.Identity;
            AlphaEffect.Projection = halfPixelOffset * projection;

            AlphaEffect.TextureEnabled = true;
            AlphaEffect.VertexColorEnabled = true;
        }

        void cancel_OnSelect(object sender, EventArgs e)
        {
            if (OnCancel != null)
            {
                OnCancel(this, null);
            }
            ExitScreen();
        }

        void no_OnSelect(object sender, EventArgs e)
        {
            if (OnNo != null)
            {
                OnNo(this, null);
            }
            ExitScreen();
        }

        void yes_OnSelect(object sender, EventArgs e)
        {
            if (OnYes != null)
            {
                OnYes(this, null);
            }
            ExitScreen();
        }

        public override void HandleInput(InputState input, GameTime gameTime)
        {
            base.HandleInput(input, gameTime);

            if (input.IsMenuSelect)
            {
                MenuEntries[selectedEntry].Select();
            }

            if (input.IsMenuCancel)
            {
                cancel_OnSelect(this, null);
            }

            if (input.IsMenuDown)
            {
                selectedEntry++;

                if (selectedEntry > MenuEntries.Count - 1)
                {
                    selectedEntry = 0;
                }
            }
            else if (input.IsMenuUp)
            {
                selectedEntry--;

                if (selectedEntry < 0)
                {
                    selectedEntry = MenuEntries.Count - 1;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);

            base.FadeBackBufferToBlack(this.TransitionAlpha * 0.5f);

            SpriteBatch batch = ScreenManager.SpriteBatch;

            AlphaEffect.Alpha = this.TransitionAlpha;

            batch.Begin(0, null, null, null, null, AlphaEffect);

            float padding = 20f;
            Vector2 center = Kristall.Viewport * 0.5f;
            Vector2 textSize = ScreenManager.Font.MeasureString(message);
            Vector2 textPosition = center - textSize * 0.5f;
            Vector2 buttonTextSize = Vector2.Zero;

            if (MenuEntries.Any())
            {
                //Measure text size of buttons
                foreach (MenuEntry entry in MenuEntries)
                {
                    Vector2 temp = ScreenManager.Font.MeasureString(entry.Text);
                    buttonTextSize.X += padding + temp.X;
                    if (buttonTextSize.Y < temp.Y)
                    {
                        buttonTextSize.Y = temp.Y;
                    }
                }
            }


            Rectangle backgroundRectangle = new Rectangle(
                (int)(textPosition.X - padding),
                (int)(textPosition.Y - padding),
                (int)(textSize.X + 2f * padding),
                (int)(textSize.Y + buttonTextSize.Y  + 3f * padding)
            );

            batch.Draw(Kristall.Blank, backgroundRectangle, backgroundColor);
            batch.DrawString(ScreenManager.Font, message, textPosition + Vector2.One, Color.Black); //1px shadow makes the text easier to read...
            batch.DrawString(ScreenManager.Font, message, textPosition, Color.White);

            if (MenuEntries.Any())
            {
                Vector2 temp2 = new Vector2(center.X - buttonTextSize.X * 0.5f, textPosition.Y + textSize.Y + padding);

                //Draw buttons
                foreach (MenuEntry entry in MenuEntries)
                {
                    Color c = Color.White;
                    if (entry == MenuEntries[selectedEntry])
                    {
                        c = Color.Yellow;
                    }

                    batch.DrawString(ScreenManager.Font, entry.Text, temp2 + Vector2.One, Color.Black); //1px shadow makes the text easier to read...
                    batch.DrawString(ScreenManager.Font, entry.Text, temp2, c);

                    temp2.X += ScreenManager.Font.MeasureString(entry.Text).X + padding;
                }
            }


            batch.End();
        }
    }
}
